package com.lhy.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhy.domain.Brand;
import com.lhy.entity.PageResult;
import com.lhy.entity.Result;
import com.lhy.entity.StatusCode;
import com.lhy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
/**
 * 品牌管理
 */
@RestController
@RequestMapping("/Brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    /**
     * 查出所有品牌
     */
    @GetMapping("/selectAll")
    public Result<List<Brand>> list() {
        return new Result<>("查询成功", brandService.selectAll());
    }
    /**
     * 增加品牌
     */
    @PostMapping("/insert")
    public Result<Integer> insertBrand(@RequestBody Brand newBrand) {
        return new Result<>("添加成功", brandService.insertBrand(newBrand));
    }

    /**
     * 修改品牌
     */
    @PostMapping("/update")
    public Result<Integer> updateBrand(@RequestBody Brand newBrand) {
        return new Result<>("修改成功", brandService.updateBrand(newBrand));
    }
    /**
     * 逻辑删品牌(多个)
     */
    @PostMapping("/delete")
        public Result<Integer> deleteBrand(String ids) {
        return new Result<>("删除成功", brandService.delete(ids));
    }
    /**
     * 修改品牌状态(单个)
     */
    @PostMapping("/status")
    public Result<Integer> changeStatus(@RequestParam("id") Long id, @RequestParam(name = "status", required = false) Integer status) {
        if (id == null) {
            return new Result<>(false, StatusCode.ERROR, "前端参数错误,id为空");
        }
        Integer result = null;
        if (status == null) {
            result = brandService.changeStatus(id);
        } else {
            result = brandService.changeStatus(id, status);
        }
        if (result == -1) {
            return new Result<>(false, StatusCode.ERROR, "品牌状态修改失败");
        }
        return new Result<>(true, StatusCode.OK, "品牌状态修改成功");
    }
    /**
     * 条件查询搜索品牌
     */
    @PostMapping("/selectList")
    public Result<List<Brand>> selectList(@RequestBody Brand brand) {
        return new Result<>("查询成功", brandService.selectList(brand));
    }
    /**
     * 分页查询
     */
    @GetMapping(value = "/search")
    public Result<PageResult<Brand>> findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult<Brand> pageResult = new PageResult<>(pageList.getTotal(), pageList.getRecords());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }
    /**
     * 品牌列表条件+分页查询
     */
    @GetMapping(value = "/pageList")
    public Result<PageResult<Brand>> pageList(@RequestBody Brand brand, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Brand> pageList = brandService.findPage(brand,page, size);
        PageResult<Brand> pageResult = new PageResult<>(pageList.getTotal(), pageList.getRecords());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }
    /**
     * 根据分类名称查询品牌列表
     * @param category
     * @return
     */
    @GetMapping("/category/{category}")
    public Result<List<Map<String,String>>>  findListByCategoryName(@PathVariable String category){
        System.out.println(category);
        List<Map<String,String>> brandList = brandService.findListByCategoryName(category);
        return new Result<>(true,StatusCode.OK,"查询成功",brandList);
    }


}
